package com.woniu.repaircenter.service.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.woniu.annotation.RequirePermission;
import com.woniu.annotation.RequireRole;
import com.woniu.repaircenter.models.dto.RepairNoticeDTO;
import com.woniu.repaircenter.models.form.AddRepairNoticeForm;
import com.woniu.repaircenter.models.form.SelectRepairNoticeForm;
import com.woniu.repaircenter.models.param.AddRepairNoticeParam;
import com.woniu.repaircenter.models.param.SelectRepairNoticeParam;
import com.woniu.repaircenter.models.param.UpdateRepairNoticeParam;
import com.woniu.repaircenter.service.service.RepairNoticeService;
import com.woniu.utils.JwtUtil;
import com.woniu.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 维修公告表 前端控制器
 * </p>
 *
 * @author 吕雨泽
 * @since 2024年12月06日
 */
@RestController
@RequestMapping("/repairNotice")
@Api(tags = "维修公告管理")
public class RepairNoticeController {

    @Autowired
    private RepairNoticeService repairNoticeService;

    /**
     * 查询公告列表
     * @param srnf 查询条件：公告标题、发布时间、发布人、公告状态、删除状态，分页参数
     * @return 包含维修公告列表的结果对象
     * @throws Exception 如果查询过程中发生异常
     */
    @PostMapping("/list")
    @ApiOperation("查询公告列表接口")
    public Result<PageInfo<RepairNoticeDTO>> list(@RequestBody @ApiParam(name = "selectRepairNoticeForm", value = "查询条件", required = true) SelectRepairNoticeForm srnf) throws Exception {
        PageInfo<RepairNoticeDTO> list = repairNoticeService.listRepairNotice(BeanUtil.toBean(srnf, SelectRepairNoticeParam.class));
        return new Result<PageInfo<RepairNoticeDTO>>()
                .setCode(200)
                .setMsg("访问成功")
                .setData(list);
    }

    /**
     * 添加公告
     * @param arnf 公告标题、公告内容、公告状态
     * @return 成功或失败的结果对象
     * @throws Exception 如果添加过程中发生异常
     */
    @PostMapping("/add")
    @ApiOperation("添加公告接口")
    @RequireRole("管理员")
    @RequirePermission("添加公告")
    public Result<String> addRepairNotice(@Valid @RequestBody @ApiParam(name = "addRepairNoticeForm", value = "公告信息", required = true) AddRepairNoticeForm arnf) throws Exception {
        Integer result = repairNoticeService.insertRepairNotice(BeanUtil.toBean(arnf, AddRepairNoticeParam.class));
        if (result == 1) {
            return new Result<String>()
                    .setCode(200)
                    .setMsg("添加成功")
                    .setData("添加成功");
        } else {
            return new Result<String>()
                    .setCode(500)
                    .setMsg("添加失败");
        }
    }

    /**
     * 修改公告或删除公告
     * @param noticeId 公告id、公告状态、删除状态、公告内容
     * @param token 用户认证令牌
     * @return 成功或失败的结果对象
     * @throws Exception 如果修改过程中发生异常
     */
    @PutMapping("/publish")
    @ApiOperation("发布公告接口")
    @RequireRole("管理员")
    @RequirePermission("发布公告")
    public Result<String> publishRepairNotice(
            @RequestParam@ApiParam(name = "noticeId", value = "公告ID", required = true) Long noticeId,
            @RequestHeader("token") String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        UpdateRepairNoticeParam bean = new UpdateRepairNoticeParam();
        bean.setNoticeId(noticeId);
        bean.setState(1);
        Integer result = repairNoticeService.updateRepairNotice(bean, adminName);
        if (result == 1) {
            return new Result<String>()
                    .setCode(200)
                    .setMsg("发布成功")
                    .setData("发布成功");
        } else {
            return new Result<String>()
                    .setCode(500)
                    .setMsg("发布失败");
        }
    }
    /**
     * 修改公告或删除公告
     * @param noticeId 公告id、公告状态、删除状态、公告内容
     * @param token 用户认证令牌
     * @return 成功或失败的结果对象
     * @throws Exception 如果修改过程中发生异常
     */
    @PutMapping("/unpublish")
    @ApiOperation("取消发布公告接口")
    @RequireRole("管理员")
    @RequirePermission("取消发布公告")
    public Result<String> unpublishRepairNotice(
            @RequestParam@ApiParam(name = "noticeId", value = "公告ID", required = true) Long noticeId,
            @RequestHeader("token") String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        UpdateRepairNoticeParam bean = new UpdateRepairNoticeParam();
        bean.setNoticeId(noticeId);
        bean.setState(0);
        Integer result = repairNoticeService.updateRepairNotice(bean, adminName);
        if (result == 1) {
            return new Result<String>()
                    .setCode(200)
                    .setMsg("取消发布成功")
                    .setData("取消发布成功");
        } else {
            return new Result<String>()
                    .setCode(500)
                    .setMsg("取消发布失败");
        }
    }

    /**
     * 修改公告或删除公告
     * @param noticeId 公告id、公告状态、删除状态、公告内容
     * @param token 用户认证令牌
     * @return 成功或失败的结果对象
     * @throws Exception 如果修改过程中发生异常
     */
    @PutMapping("/delete")
    @ApiOperation("删除公告接口")
    @RequireRole("管理员")
    @RequirePermission("删除公告")
    public Result<String> deleteRepairNotice(
            @RequestParam@ApiParam(name = "noticeId", value = "公告ID", required = true) Long noticeId,
            @RequestHeader("token") String token) throws Exception {
        String adminName = JwtUtil.parseToken(token).get("adminName").toString();
        UpdateRepairNoticeParam bean = new UpdateRepairNoticeParam();
        bean.setNoticeId(noticeId);
        bean.setDelFlag(1);
        Integer result = repairNoticeService.updateRepairNotice(bean, adminName);
        if (result == 1) {
            return new Result<String>()
                    .setCode(200)
                    .setMsg("删除成功")
                    .setData("删除成功");
        } else {
            return new Result<String>()
                    .setCode(500)
                    .setMsg("删除失败");
        }
    }
    /**
     * 查询公告详情
     * @param noticeId 公告id
     * @return 包含公告详情的结果对象
     * @throws Exception 如果查询过程中发生异常
     */
    @GetMapping("/detail")
    @ApiOperation("查询公告详情接口")
    public Result<RepairNoticeDTO> getNotice(@RequestParam @ApiParam(name = "noticeId", value = "公告ID", required = true) Long noticeId) throws Exception {
        RepairNoticeDTO repairNoticeDTO = repairNoticeService.selectRepairNotice(noticeId);
        return new Result<RepairNoticeDTO>()
                .setCode(200)
                .setMsg("访问成功")
                .setData(repairNoticeDTO);
    }
}
